<?php
/**
 * Javascript View Helper.
 * @package halo
 */

hc_core_ClassLoader::load('halo_AbstractExternalLinkViewHelper');

/**
 * Javascript View Helper.
 *
 * The Javascript View Helper helps manage the inclusion of external Javascript files.
 * It also provides some additional helper methods such as unique DOM ID generation.
 *
 * To include external Javascript from one location (from <head/>, for example), use
 * the {@link inc()} method. This pushes the requested script URI onto the list of
 * Javascript URIs to load by {@link renderIncluded()}.
 *
 * <code>
 * <!-- Push /js/sample.js onto the Javascript include stack. -->
 * <?php $javascript->inc('/js/sample.js'); ?>
 * </code>
 *
 * All of the external Javascript files included in this way can be rendered at
 * by calling {@link renderIncluded()}.
 *
 * <code>
 * <!-- To render all included scripts <script></script> tags... -->
 * <?php print $javascript->renderIncluded(); ?>
 * </code>
 *
 * To include external Javascript from the current location in the template ("here"),
 * use the {@link inline()} method. This method renders the <script></script> tag
 * immediately.
 *
 * <code>
 * <!-- This renders the <script></script> tag inline "here" in the template. -->
 * <?php print $javascript->inline('/js/sample.js'); ?>
 * </code>
 *
 */
class halo_JavascriptViewHelper extends halo_AbstractExternalLinkViewHelper {

    /**
     * Unique counter for DOM ID generation.
     * @var int
     */
    private $counter = 0;

    /**
     * Default ID seed if one is not specified.
     * @var string
     */
    private $defaultIdSeed = null;

    /**
     * Constructor.
     * @param halo_HttpRequest $httpRequest The request.
     * @param halo_HttpResponse $httpResponse The response.
     */
    public function __construct(halo_HttpRequest $httpRequest, halo_HttpResponse $httpResponse) {
        parent::__construct($httpRequest, $httpResponse);
        $this->defaultIdSeed = uniqid();
    }

    /**
     * Render external Javascript links.
     *
     * Given the URI and options, creates the appropriate script tag. For example, for
     * a URI passed as "/js/sample.js", the following would be rendered:
     *
     * <code>
     * <script type="text/javascript" src="/js/sample.js"></script>
     * </code>
     * @param string $uri Sanitized URI
     * @param array $options Options.
     * @return string Rendered link.
     */
    protected function renderExternalLink($uri, $options) {
        return '<script type="text/javascript" src="' . $uri . '"></script>';
    }

    /**
     * Generate DOM ID.
     *
     * Used to generate a unique DOM ID that can be accessed by Javascript.
     *
     * <code>
     * <?php $myButtonId = $javascript->generateDomId(); ?>
     * <button id="<?php $s->p($myButtonid); ?>" value="Press Me" />
     * <script type="text/javascript">
     * $('#<?php $s->p($myButtonId); ?>').click(function(){
     *     alert('Been clicked!');
     * });
     * </script>
     * </code>
     * @param string $idSeed ID Seed.
     * @return string Unique DOM ID.
     */
    public function generateDomId($idSeed = null) {
        if ( $idSeed === null ) $idSeed = $this->defaultIdSeed;
        return '__' . $idSeed . '_h_gDI_' . $this->counter++ . '_' . md5(rand());
    }
}
?>
